package slidingwindow;

public class TwoSum {
    public static void main(String[] args) {
        int[] price = {3, 9, 12, 15};
        int target = 18;
        System.out.println(twoSum(price, target));
    }

    //暴力解法
    public static int[] twoSum1(int[] price, int target) {
        for (int i = 0; i < price.length; i++) {
            for (int j = i + 1; j < price.length; j++) {
                if (price[i] + price[j] == target) {
                    return new int[]{price[i], price[j]};
                }
            }
        }
        return new int[]{0};
    }

    public static int[] twoSum(int[] price, int target) {
        int left = 0, right = price.length - 1;
        while (left < right) {
            int sum = price[left] + price[right];
            if (sum > target) right--;
            else if (sum < target) left++;
            else return new int[]{price[left], price[right]};
        }
        return new int[]{0};
    }
}
